from db import BaseModel
from django.conf import settings
from django.db import models


class Link(BaseModel):
    """链接"""
    url = models.URLField('URL', unique=True)

    class Meta:
        verbose_name = verbose_name_plural = 'URL链接'
        ordering = ('-date_created',)

    def __str__(self):
        return self.url


class Bookmark(BaseModel):
    """书签"""
    title = models.CharField('标题', max_length=200)
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name='用户',
        on_delete=models.CASCADE
    )
    link = models.ForeignKey(
        Link,
        verbose_name='URL',
        on_delete=models.CASCADE
    )
    # is_privated = models.BooleanField('是否私有', default=False)
    num_likes = models.PositiveIntegerField('点赞数', default=0)

    class Meta:
        verbose_name = verbose_name_plural = '书签'
        ordering = ('-date_created',)

    def __str__(self):
        return self.title[:20]


class Tag(BaseModel):
    """标签模型"""
    name = models.CharField('名字', max_length=64, unique=True)
    # 一个标签可以出现在多个书签中，一个书签可以有多个标签
    bookmarks = models.ManyToManyField(Bookmark, verbose_name='书签')

    class Meta:
        verbose_name = verbose_name_plural = '标签'
        ordering = ('-date_created',)

    def __str__(self):
        return self.name


class Like(BaseModel):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        verbose_name='用户',
        on_delete=models.CASCADE
    )
    bookmark = models.ForeignKey(
        Bookmark,
        verbose_name='书签',
        on_delete=models.CASCADE
    )

    class Meta:
        verbose_name = verbose_name_plural = '点赞'
        unique_together = (('user', 'bookmark'),)

    def __str__(self):
        return f'{self.user!s}点赞{self.bookmark!s}'
